﻿using System.Collections.Generic;
using System.Linq;
using RBOSS.Business.CustomerModule;
using RBOSS.Business.Products;
using RBOSS.Data;

namespace RBOSS.Business.ShoppingCarts
{
    internal class ShoppingCart:IShoppingCart
    {
        private readonly List<IProduct> _products;
        readonly RBOSSEntities _dataContext = new RBOSSEntities();
        private readonly ICustomer _customer;
        public ShoppingCart()
        {
            _products = new List<IProduct>();
            _customer = CustomerSingleton.GetCustomer();
        }

        public void AddItemToSessionCart(IProduct product, int quantity)
        {
            product.Qunatity = quantity;
            _products.Add(product);
        }

        public void AddItemToCart(IProduct product,int quantity)
        {
            var p = (from prod in _products where prod.ProductId == product.ProductId select prod).FirstOrDefault();

            if (p != null)
            {
                p.Qunatity += quantity;
                if (_customer.IsLogin)
                {
                    var shoppingCart = (from cart in _dataContext.ShoppingCarts where cart.ProductId==product.ProductId select cart).FirstOrDefault();
                    if (shoppingCart != null)
                    {
                        shoppingCart.Quantity += quantity;
                    }
                    _dataContext.SaveChanges();
                }
            }
            else
            {
                product.Qunatity = quantity;
                _products.Add(product);
                if (_customer.IsLogin)
                {
                    var shoppingCart = new Data.ShoppingCart
                    {
                        ProductId = product.ProductId,
                        Quantity = product.Qunatity,
                        UserId = _customer.CustomerDetails.CustomerId
                    };
                    _dataContext.ShoppingCarts.Add(shoppingCart);
                    _dataContext.SaveChanges();
                }
            }
            
            
            
        }

        public void RemoveItemFromCart(int productId)
        {
            
            foreach (var product in _products)
            {
                if (product.ProductId == productId)
                {
                    _products.Remove(product);
                    if (_customer.IsLogin)
                    {
                        DeleteProductFromDatabase(product);
                    }
                    break;
                }
            }
        }

        private void DeleteProductFromDatabase(IProduct product)
        {
            var prod = (from cart in _dataContext.ShoppingCarts where cart.ProductId == product.ProductId select cart).FirstOrDefault();
            _dataContext.ShoppingCarts.Remove(prod);
            _dataContext.SaveChanges();
        }

        public decimal GetTotalPrice()
        {
            var totalPrice = _products.Sum(p => p.Total);
            return totalPrice;
        }

        public void ClearItemFromCart()
        {
            var customer = CustomerSingleton.GetCustomer();
            if (customer.CustomerDetails != null)
            {
                var customerId = customer.CustomerDetails.CustomerId;
                if (customerId > 0)
                {
                    var shoppingCart = from s in _dataContext.ShoppingCarts where s.UserId == customerId select s;
                    if (shoppingCart.Any())
                    {
                        _dataContext.ShoppingCarts.RemoveRange(shoppingCart);
                        _dataContext.SaveChanges();
                    }
                }
            }
            _products.Clear();
        }

        public void ClearItemsFromSessionCart()
        {
            _products.Clear();
            
        }

        public void SaveCartToDataBase()
        {
            var customerId = _customer.CustomerDetails.CustomerId;
            foreach (var product in _products)
            {
                var cart = new Data.ShoppingCart();
                cart.ProductId = product.ProductId;
                cart.Quantity = product.Qunatity;
                cart.UserId = customerId;

                _dataContext.ShoppingCarts.Add(cart);
            }
            _dataContext.SaveChanges();
        }

        public List<IProduct> GetProducts()
        {
            return _products;
        }
    }
}